package org.xx.armory.swing.builders;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xx.armory.objectparser.ParsedObjectBuilder;

import java.nio.file.Paths;

import static org.apache.commons.lang3.StringUtils.trimToEmpty;

public class ImageModelBuilder
        extends ParsedObjectBuilder<ImageModel> {
    private final Logger logger = LoggerFactory.getLogger(ImageModelBuilder.class);

    private String name;
    private String src;

    public ImageModelBuilder setName(
            String name
    ) {
        this.name = name;
        return this;
    }

    public ImageModelBuilder setSrc(
            String src
    ) {
        this.src = src;
        return this;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ImageModel build() {
        final String src;
        if (getLineInfo() != null) {
            src = Paths.get(getLineInfo().getPath()).resolveSibling(this.src).toString();
            logger.trace("resolve src \"{}\" to \"{}\"", this.src, src);
        } else {
            src = trimToEmpty(this.src);
        }
        return new ImageModel(getLineInfo(), this.name,  src);
    }
}
